package com.jiankangjin.transsystem.search;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 键值仓库
 */
public class DataBaseMap {
	private static ConcurrentHashMap<String, SearchOrderIds> staticMap = new ConcurrentHashMap<String, SearchOrderIds>();;
	
	public DataBaseMap() {
		
	}
	
	public static String createKey() {
		long mi = System.currentTimeMillis();
		String key = "2" + mi;
		long d = Long.parseLong(key);
		long sum = 0;
		while(d>0) {
			sum += d%10;
			d = d/10;
		}
		sum = sum%10;
		key += sum;
		return key;
	}
	
	public static void put(String key, SearchOrderIds o) {
		staticMap.put(key, o);
	}
	
	public static List<String> get(String key, int page, int quantity) {
		List<Map<String, Object>> all = staticMap.get(key).getAllId();
		System.out.print("key="+key+"的查询记录，共有"+all.size()+"条数据:");
		int cutPage = all.size()%quantity==0 ? all.size()/quantity : all.size()/quantity+1;
		System.out.print("按每页"+quantity+"条分成了"+cutPage+"页");
		int qut = 0;
		if (page == 0) {
			qut = 0;
		} else if (page < cutPage) {
			qut = quantity;
		} else if(page == cutPage) {
			qut = (all.size()%quantity)!=0 ? (all.size()%quantity) : quantity;
		} else {
			qut = 0;
		}
		System.out.print("当前查询的第"+page+"页有"+qut+"条\n");
		List<String> list = new ArrayList<String>();
		for (int i = 0,j=(page-1)*quantity; i < qut; i++, j++) {
			list.add((String) all.get(j).get("ID"));
		}
		return list;
	}
	
	public static Map<String, Object> getWhereMap(String key) {
		return staticMap.get(key).getWhereMap();
	}
	
	public static void clean() {
		staticMap.clear();
	}
	
	public static int size() {
		return staticMap.size();
	}
	/*
	public static void main(String[] args) {
		//传入：我需要第page页的quantity条数据
		int page = 4;
		int quantity = 33;
		
		//设置数据共有多少条
		int a = 100;
		List<String> all = new ArrayList<String>();
		for (int i = 0; i < a; i++) {
			all.add("[a"+i+"]");
		}
		System.out.println("共有"+all.size()+"条数据:");
		System.out.println(all);
		int cutPage = all.size()%quantity==0 ? all.size()/quantity : all.size()/quantity+1;
		System.out.println("按每页"+quantity+"条分成了"+cutPage+"页");
		int qut = 0;
		if (page < cutPage) {
			qut = quantity;
		} else if(page == cutPage) {
			qut = all.size()%quantity;
		} else {
			qut = 0;
		}
		System.out.println("第"+page+"页有"+qut+"条");
		List<String> list = new ArrayList<String>();
		for (int i = 0,j=(page-1)*quantity; i < qut; i++, j++) {
			list.add(all.get(j));
		}
		System.out.println(list);
	}*/
}
